unit uniManCheque;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uniManPadrao, DB, ADODB, Grids, DBGrids, StdCtrls, ExtCtrls,
  Buttons;

type
  TfrmManCheque = class(TfrmManPadrao)
    Panel1: TPanel;
    Label7: TLabel;
    Label9: TLabel;
    Panel6: TPanel;
    Panel8: TPanel;
    QR_GridNOME: TWideStringField;
    QR_Gridcd_cheque_fatura: TAutoIncField;
    QR_Gridcd_pagamento: TIntegerField;
    QR_Gridnr_cheque: TWideStringField;
    QR_Gridnr_conta: TWideStringField;
    QR_Gridagencia: TWideStringField;
    QR_Griddt_compensacao: TDateTimeField;
    QR_Gridvalor: TBCDField;
    QR_Gridstatus: TWideStringField;
    QR_Griddt_confirmacao: TDateTimeField;
    QR_Gridobs: TWideStringField;
    QR_GridsValor: TStringField;
    QR_GridsStatus: TStringField;
    Label3: TLabel;
    edtNrCheque: TEdit;
    QR_GridCD_PEDIDO: TWideStringField;
    rbTodos: TRadioButton;
    rbDevolvido: TRadioButton;
    rbCompensado: TRadioButton;
    QR_Gridcd_banco: TWideStringField;
    QR_Gridds_titular_cheque: TWideStringField;
    QR_Gridds_banco: TWideStringField;
    btnNovo: TSpeedButton;
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure QR_GridCalcFields(DataSet: TDataSet);
    procedure btnIncluirClick(Sender: TObject);
    procedure btnPesquisaClick(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
  private
    pCdPedido,
    pRepresentante,
    pNrCheque,
    pStatus : String;
    procedure p_Select;
    function f_StatusGrid(pDtCompensacao:TDate;pDtConfirmacao:String; pStatus:String):Integer;
  public
    { Public declarations }
  end;

var
  frmManCheque: TfrmManCheque;

implementation

uses uniFuncBD, UniFuncoes, uniCadCheque, uniDtmSisnnovi;

{$R *.dfm}

procedure TfrmManCheque.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var  iStatus : integer;
begin  
  //Pinta o status do grid
  {
   0 - Branco: Cheque em aberto
   1 - Verde: Cheque compensado
   2 - Amarelo: Cheque vencido
   3 - Vermelho: Cheque devolvido
  }
  if Column.FieldName = 'sStatus' then
  begin
    iStatus := f_StatusGrid(Column.Field.DataSet.FieldByName('DT_COMPENSACAO').AsDateTime,Column.Field.DataSet.FieldByName('DT_CONFIRMACAO').AsString,Column.Field.DataSet.FieldByName('STATUS').AsString );
    case iStatus of
      0:
      begin
         DBGrid1.Canvas.Brush.Color := clWindow;
         DBGrid1.Canvas.Font.Color := clWindow;
      end;
      1:
      begin
        DBGrid1.Canvas.Brush.Color := clGreen;
        DBGrid1.Canvas.Font.Color := clGreen;
      end;
      2:
      begin
       DBGrid1.Canvas.Brush.Color := clYellow;
       DBGrid1.Canvas.Font.Color := clYellow;
      end;
      3:
      begin
        DBGrid1.Canvas.Brush.Color := clRed;
        DBGrid1.Canvas.Font.Color := clRed;
      end;
      4:
      begin
        DBGrid1.Canvas.Brush.Color := clNavy;
        DBGrid1.Canvas.Font.Color := clNavy;
      end; 
    end;
  end;
  DBGrid1.DefaultDrawColumnCell(rect,DataCol,Column,State);
end;

function TfrmManCheque.f_StatusGrid(pDtCompensacao:TDate;pDtConfirmacao: String;pStatus:String): Integer;
var iRet:Integer;
begin
  {
   0 - Branco: Cheque em aberto
   1 - Verde: Cheque compensado
   2 - Amarelo: Cheque vencido
   3 - Vermelho: Cheque devolvido
  }

  if pStatus = 'D' then
  begin
    iRet := 3;
  end
  else
  if pDtCompensacao <> null then
  begin
    iRet := 1;
  end
  else
  if pDtCompensacao < Date then
  begin
    iRet := 2;
  end
  else
  begin
    iRet := 0;
  end;
  Result := iRet;
end;

procedure TfrmManCheque.p_Select;
var sSQL : String;
begin
  sSQL := ' SELECT DISTINCT R.NOME,  P.CD_PEDIDO, CF.*, B.ds_banco '+
          ' FROM (((((TB_CHEQUE_FATURA CF '+
          ' INNER JOIN TB_FATURA_PARCELA FP '+
          ' ON CF.CD_PAGAMENTO = FP.CD_PAGAMENTO) '+
          ' INNER JOIN TB_FATURA F '+
          ' ON FP.CD_FATURA = F.CD_FATURA) '+
          ' INNER JOIN TB_PEDIDO P '+
          ' ON F.CD_PEDIDO = P.CD_PEDIDO) '+
          ' INNER JOIN TB_REPRESENTANTE R '+
          ' ON P.CD_REPRESENTANTE = R.CD_REPRESENTANTE) '+
          ' LEFT JOIN TB_BANCO B '+
          ' ON CF.CD_BANCO = B.CD_BANCO) '+
          ' WHERE 1=1 '+ pCdPedido + pRepresentante + pNrCheque + pStatus +
          ' UNION '+
          ' SELECT DISTINCT ''- CHEQUE SEM FATURA -'' AS NOME, '''' AS CD_PEDIDO, CF.*, B.DS_BANCO '+
          ' FROM (TB_CHEQUE_FATURA CF LEFT JOIN TB_BANCO B ON CF.CD_BANCO = B.CD_BANCO) '+
          ' WHERE CD_PAGAMENTO = 1 '+
          pNrCheque + pStatus;
          
  QR_Grid.Close;
  QR_Grid.SQL.Clear;
  QR_Grid.SQL.Add(sSQL);
  QR_Grid.Open;

  //Zerar parametros
  pCdPedido := '';
  pRepresentante := '';
  pNrCheque := '';
  pStatus := ''; 
end;

procedure TfrmManCheque.QR_GridCalcFields(DataSet: TDataSet);
begin
  inherited;
  QR_Grid.FieldByName('sValor').AsString := formatarValor(QR_Grid.FieldByName('valor').AsString);
end;

procedure TfrmManCheque.btnIncluirClick(Sender: TObject);
begin
  try
    Application.CreateForm(TfrmCadCheque, frmCadCheque);
    frmCadCheque.gCdCheque := QR_Grid.FieldByName('cd_cheque_fatura').AsString;
    frmCadCheque.gbNovoCheque.Visible := False;
    frmCadCheque.ShowModal;
  finally
    frmCadCheque.Free;
  end;
end;

procedure TfrmManCheque.btnPesquisaClick(Sender: TObject);
begin  
  if edtCodigo.Text <> '' then
    pCdPedido := ' AND P.CD_PEDIDO = '+ Quotedstr(edtCodigo.Text);

  if edtDescricao.Text <> '' then
    pRepresentante := ' AND R.nome LIKE '+QuotedStr(edtDescricao.Text);

  if edtNrCheque.Text <> '' then
    pNrCheque := ' AND NR_CHEQUE = '+QuotedStr(edtNrCheque.text);

  if rbDevolvido.Checked then
    pStatus := ' AND STATUS = '+QuotedStr('D')
  else
  if rbCompensado.Checked then
    pStatus := ' AND STATUS = '+QuotedStr('C');

  p_Select;

end;      
procedure TfrmManCheque.btnNovoClick(Sender: TObject);
begin
  try
    Application.CreateForm(TfrmCadCheque, frmCadCheque);
    frmCadCheque.gbNovoCheque.Visible := True;
    frmCadCheque.gCdCheque := '';
    frmCadCheque.ShowModal;
  finally
    frmCadCheque.Free;
  end;

end;

end.
